home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / ircii2-6.zip / SRC\IRCII-2.6\SOURCE\QUEUE.C < prev    next >
C/C++ Source or Header  |  1994-12-28  |  8KB  |  414 lines

  1. /*
  2.  *  queue.c - The queue command
  3.  *  
  4.  *  Queues allow for future batch processing
  5.  *
  6.  *  Syntax:  /QUEUE -DO -SHOW -LIST -NO_FLUSH -DELETE -FLUSH <name> {commands}
  7.  *
  8.  *  Written by Jeremy Nelson
  9.  *
  10.  *  Copyright (C) 1993.  See the copyright file and all that rot
  11.  */
  12.  
  13. #ifndef lint
  14. static    char    rcsid[] = "@(#)$Id: queue.c,v 1.3 1994/07/30 17:31:55 mrg Stab $";
  15. #endif
  16.  
  17. #include "irc.h"
  18. #include "alias.h"
  19. #include "ircaux.h"
  20. #include "debug.h"
  21. #include "output.h"
  22. #include "edit.h"
  23.  
  24. typedef    struct CmdListT
  25. {
  26.         struct CmdListT    *next;
  27.         char        *what;
  28. } CmdList;
  29.  
  30. typedef    struct    QueueT
  31. {
  32.         struct QueueT   *next;
  33.         struct CmdListT *first;
  34.         char     *name;
  35. } Queue;
  36.  
  37.     void    queuecmd __P((char    *, char *, char *));
  38. static    Queue    *lookup_queue __P((Queue *, char *));
  39. static    CmdList    *walk_commands __P((Queue *));
  40. static    Queue    *make_new_queue __P((Queue *, char *));
  41. static    int    add_commands_to_queue __P((Queue *, char *what, char *));
  42. static    int    delete_commands_from_queue __P((Queue *, int));
  43. static    Queue    *remove_a_queue __P((Queue *));
  44. static    void    flush_queue __P((Queue    *));;
  45. static    Queue    *do_queue __P((Queue *, int));
  46. static    void    display_all_queues __P((Queue *));
  47. static    void    print_queue __P((Queue *));
  48. static    int    num_entries __P((Queue *));
  49.  
  50. extern    void 
  51. queuecmd(cmd, args, subargs)
  52.     char    *cmd,
  53.         *args,
  54.         *subargs;
  55. {
  56.     Queue    *tmp;
  57.     char    *arg = (char *) 0,
  58.         *name = (char *) 0,
  59.         *startcmds = (char *) 0,
  60.         *cmds = (char *) 0;
  61.     int    noflush = 0,
  62.         runit = 0, 
  63.         list = 0,
  64.         flush = 0,
  65.         remove_by_number = 0,
  66.         commands = 1,
  67.         number = 0;
  68.     static    Queue    *Queuelist;
  69.     
  70.     /* If the queue list is empty, make an entry */
  71.     if (Queuelist == (Queue *) 0)
  72.         Queuelist = make_new_queue((Queue *) 0, "Top");
  73.  
  74.     if ((startcmds = index(args, '{')) == (char *) 0) /* } */
  75.         commands = 0;
  76.     else
  77.         *(startcmds-1) = '\0';
  78.  
  79.     while ((arg = upper(next_arg(args, &args))) != (char *) 0)
  80.     {
  81.         if (*arg == '-')
  82.         {
  83.             *arg++ = '\0';
  84.             if (!strcmp(arg, "NO_FLUSH"))
  85.                 noflush = 1;
  86.             else
  87.             if (!strcmp(arg, "SHOW"))
  88.             {
  89.                 display_all_queues(Queuelist);
  90.                 return;
  91.             }
  92.             else
  93.             if (!strcmp(arg, "LIST"))
  94.                 list = 1;
  95.             else
  96.             if (!strcmp(arg, "DO"))
  97.                 runit = 1;
  98.             else
  99.             if (!strcmp(arg, "DELETE"))
  100.                 remove_by_number = 1;
  101.             else
  102.             if (!strcmp(arg, "FLUSH"))
  103.                 flush = 1;
  104.         }
  105.         else
  106.         {
  107.             if (name)
  108.                 number = atoi(arg);
  109.             else
  110.                 name = arg;
  111.         }
  112.     }
  113.  
  114.     if (name == (char *) 0)
  115.         return;
  116.  
  117.     /* Find the queue based upon the previous queue */
  118.     tmp = lookup_queue(Queuelist, name);
  119.  
  120.     /* if the next queue is empty, then we need to see if 
  121.        we should make it or output an error */
  122.     if ((tmp->next) == (Queue *) 0)
  123.     {
  124.         if (commands)
  125.             tmp->next = make_new_queue((Queue *) 0, name);
  126.         else
  127.         {
  128.             yell ("QUEUE: (%s) no such queue",name);
  129.             return;
  130.         }
  131.     }
  132.     if (remove_by_number == 1)
  133.     {
  134.         if (delete_commands_from_queue(tmp->next,number))
  135.             tmp->next = remove_a_queue(tmp->next);
  136.     }
  137.     if (list == 1)
  138.     {
  139.         print_queue(tmp->next);
  140.     }
  141.     if (runit == 1)
  142.     {
  143.         tmp->next = do_queue(tmp->next, noflush);
  144.     }
  145.     if (flush == 1)
  146.     {
  147.         tmp->next = remove_a_queue(tmp->next);
  148.     }
  149.     if (startcmds)
  150.     {
  151.         int booya;
  152.  
  153.         if ((cmds = next_expr(&startcmds, '{')) == (char *) 0) /* } */
  154.         {
  155.             yell ("QUEUE: missing closing brace");
  156.             return;
  157.         }
  158.         booya = add_commands_to_queue (tmp->next, cmds, subargs);
  159.         say ("QUEUED: %s now has %d entries",name, booya);
  160.     }
  161. }
  162.  
  163. /*
  164.  * returns the queue BEFORE the queue we are looking for
  165.  * returns the last queue if no match
  166.  */
  167. static    Queue    *
  168. lookup_queue(queue, what)
  169.     Queue    *queue;
  170.     char    *what;
  171. {
  172.     Queue    *tmp = queue;
  173.  
  174.     upper(what);
  175.  
  176.     while (tmp->next)
  177.     {
  178.         if (!strcmp(tmp->next->name, what))
  179.             return tmp;
  180.         else
  181.             if (tmp->next)
  182.                 tmp = tmp->next;
  183.             else
  184.                 break;
  185.     }
  186.     return tmp;
  187. }
  188.  
  189. /* returns the last CmdList in a queue, useful for appending commands */
  190. static    CmdList    *
  191. walk_commands(queue)
  192.     Queue     *queue;
  193. {
  194.     CmdList    *ctmp;
  195.     
  196.     if (!queue)
  197.         return (CmdList *) 0;
  198.  
  199.     ctmp = queue->first;
  200.     if (ctmp)
  201.     {
  202.         while (ctmp->next)
  203.             ctmp = ctmp->next;
  204.         return ctmp;
  205.     }
  206.     return (CmdList *) 0;
  207. }
  208.  
  209. /*----------------------------------------------------------------*/
  210. /* Make a new queue, link it in, and return it. */
  211. static    Queue    *
  212. make_new_queue(afterqueue, name)
  213.     Queue    *afterqueue;
  214.     char    *name;
  215. {
  216.     Queue    *tmp;
  217.  
  218.     if (!afterqueue || !name)
  219.         return (Queue *) 0;
  220.     tmp = (Queue *) new_malloc(sizeof(Queue));
  221.     upper(name);
  222.  
  223.     tmp->next = afterqueue;
  224.     tmp->first = (CmdList *) 0;
  225.     tmp->name = (char *) 0;
  226.     malloc_strcpy(&tmp->name, name);
  227.     return tmp;
  228. }
  229.     
  230. /* add a command to a queue, at the end of the list */
  231. /* expands the whole thing once and stores it */
  232. static    int
  233. add_commands_to_queue(queue, what, subargs)
  234.     Queue    *queue;
  235.     char    *what;
  236.     char    *subargs;
  237. {
  238.     CmdList *ctmp = walk_commands(queue);
  239.     char    *list = (char *) 0,
  240.         *sa;
  241.     int     args_flag = 0;
  242.     
  243.     sa = subargs ? subargs : " ";
  244.     list = expand_alias((char *) 0, what, sa, &args_flag, (char **) 0);
  245.     if (!ctmp)
  246.     {
  247.         queue->first = (CmdList *) new_malloc(sizeof(CmdList));
  248.         ctmp = queue->first;
  249.     }
  250.     else
  251.     {
  252.         ctmp->next = (CmdList *) new_malloc(sizeof(CmdList));
  253.         ctmp = ctmp->next;
  254.     }
  255.      ctmp->what = (char *) 0;
  256.     malloc_strcpy(&ctmp->what, list);
  257.     ctmp->next = (CmdList *) 0;
  258.     return num_entries(queue);
  259. }
  260.  
  261.  
  262. /* remove the Xth command from the queue */
  263. static    int
  264. delete_commands_from_queue(queue, which)
  265.     Queue    *queue;
  266.     int    which;
  267. {
  268.     CmdList *ctmp = queue->first;
  269.     CmdList *blah;
  270.     int x;
  271.  
  272.     if (which == 1)
  273.         queue->first = ctmp->next;
  274.     else
  275.     {
  276.         for (x=1;x<which-1;x++)
  277.         {
  278.             if (ctmp->next) 
  279.                 ctmp = ctmp->next;
  280.             else 
  281.                 return 0;
  282.         }
  283.         blah = ctmp->next;
  284.         ctmp->next = ctmp->next->next;
  285.         ctmp = blah;
  286.     }
  287.     new_free(&ctmp->what);
  288.     new_free(&ctmp);
  289.     if (queue->first == (CmdList *) 0)
  290.         return 1;
  291.     else
  292.         return 0;
  293. }
  294.  
  295. /*-------------------------------------------------------------------*/
  296. /* flush a queue, deallocate the memory, and return the next in line */
  297. static    Queue    *
  298. remove_a_queue(queue)
  299.     Queue    *queue;
  300. {
  301.     Queue *tmp;
  302.  
  303.     tmp = queue->next;
  304.     flush_queue(queue);
  305.     new_free(&queue);
  306.     return tmp;
  307. }
  308.  
  309. /* walk through a queue, deallocating the entries */
  310. static    void
  311. flush_queue(queue)
  312.     Queue    *queue;
  313. {
  314.     CmdList    *tmp,
  315.         *tmp2;
  316.  
  317.     tmp = queue->first;
  318.  
  319.     while (tmp != (CmdList *) 0)
  320.     {
  321.         tmp2 = tmp;
  322.         tmp = tmp2->next;
  323.         if (tmp2->what != (char *) 0)
  324.             new_free(&tmp2->what);
  325.         if (tmp2)
  326.             new_free(&tmp2);
  327.     }
  328. }
  329.  
  330. /*------------------------------------------------------------------------*/
  331. /* run the queue, and if noflush, then return the queue, else return the
  332.    next queue */
  333. static    Queue    *
  334. do_queue(queue, noflush)
  335.     Queue    *queue;
  336.     int    noflush;
  337. {
  338.     CmdList    *tmp;
  339.     
  340.     tmp = queue->first;
  341.     
  342.     do
  343.     {
  344.         if (tmp->what != (char *) 0)
  345.             parse_line((char *) 0, tmp->what, empty_string, 0, 0);
  346.         tmp = tmp->next;
  347.     }
  348.     while (tmp != (CmdList *) 0);
  349.  
  350.     if (!noflush) 
  351.         return remove_a_queue(queue);
  352.     else
  353.         return queue;
  354. }
  355.  
  356. /* ---------------------------------------------------------------------- */
  357. /* output the contents of all the queues to the screen */
  358. static    void
  359. display_all_queues(queue)
  360.     Queue    *queue;
  361. {
  362.     Queue *tmp;
  363.  
  364.     if (!queue)
  365.         return;
  366.  
  367.     tmp = queue->next;
  368.     while (tmp)
  369.     {
  370.         print_queue(tmp);
  371.         if (tmp->next == (Queue *) 0)
  372.             return;
  373.         else
  374.             tmp = tmp->next;
  375.     }
  376.     yell("QUEUE: No more queues");
  377. }
  378.  
  379. /* output the contents of a queue to the screen */
  380. static    void
  381. print_queue(queue)
  382.     Queue    *queue;
  383. {
  384.     CmdList *tmp;
  385.     int     x = 0;
  386.     
  387.     tmp = queue->first;
  388.     while (tmp != (CmdList *) 0)
  389.     {
  390.         if (tmp->what)
  391.             say ("<%s:%2d> %s",queue->name,++x,tmp->what);
  392.         tmp = tmp->next;
  393.     }
  394.     say ("<%s> End of queue",queue->name);
  395. }
  396.  
  397. /* return the number of entries in a queue */
  398. static    int
  399. num_entries(queue)
  400.     Queue    *queue;
  401. {
  402.     int x = 1;
  403.     CmdList *tmp;
  404.  
  405.     if ((tmp = queue->first) == (CmdList *) 0) 
  406.         return 0;
  407.     while (tmp->next)
  408.     {
  409.         x++;
  410.         tmp = tmp->next;
  411.     }
  412.     return x;
  413. }
  414.